﻿
using NPoco;
using System.Linq;
using YYBlog.Core.Entity;

namespace YYBlog.Core.ViewProjections.Home
{
    public class BlogPostDetailsBindingModel
    {
        public string Permalink { get; set; }
    }

    public class BlogPostDetailsViewModel
    {
        public BlogPost BlogPost { get; set; }

        //public BlogComment[] Comments { get; set; }
    }

    public class BlogPostDetailsViewProjection : IViewProjection<BlogPostDetailsBindingModel, BlogPostDetailsViewModel>
    {
        private readonly Database _db;

        public BlogPostDetailsViewProjection(Database db)
        {
            _db = db;
        }

        public BlogPostDetailsViewModel Project(BlogPostDetailsBindingModel input)
        {
            var post = _db.Fetch<BlogPost>(" where TitleSlug=@0", input.Permalink).FirstOrDefault();
            if (post == null)
                return null;
             
            _db.Update(post);

            //var comments = _db.Select<BlogComment>("from " + DBTableNames.BlogComments + " where PostId ==?", post.Id)
            //                        .OrderBy(o => o.CreatedTime)
            //                        .ToArray();

            return new BlogPostDetailsViewModel
            {
                BlogPost = post,
               
            };
        }
    }
}